<template>
  <div class="school">
    <h2>学校名称:{{ name }}</h2>
    <h2>学校地址:{{ address }}</h2>
  </div>
</template>

<script>
import pubsub from "pubsub-js";
export default {
  name: "School",
  data() {
    return {
      name: "三峡学院",
      address: "重庆",
    };
  },
  mounted() {
    //需要注意的是这里面的this是undefined，所以需要写成箭头函数，使this变为vc
    this.pubId = pubsub.subscribe("hello", (msgName, data) => {
      console.log(
        "有人发布了hello消息，hello消息的回调函数被执行了",
        msgName,
        data
      );
    });
  },
  beforeDestroy() {
    //取消订阅的方式有点类似于定时器，用id取消
    pub.unsubscribe(this.pubId);
  },
};
</script>
<style scoped>
.school {
  padding: 5px;
  background-color: skyblue;
}
</style>

